package org.melanesia.ref;

/**
 * A wrapper class for objects implementing releasable resources. Releasable
 * resources are being attached to the {@link ObjectReference} as stated in
 * {@link ReferenceMonitor}.
 *
 * Because each resource can be of different type, the generic wrapper class is
 * introduced in order to allow users to implement their own finalizers for the
 * resource.
 *
 * @author marcin.kielar
 *
 * @param <T>
 */
public abstract class ReleasableResource<T> {

    /** The releasable resource. */
    private final T resource;

    /**
     * Constructs new releasable resource wrapper.
     *
     * @param resource
     *            resource
     */
    public ReleasableResource(final T resource) {
        this.resource = resource;
    }

    /**
     * Unwraps the resource.
     *
     * @return the original resource
     */
    protected final T get() {
        return resource;
    }

    /**
     * Releases a resource.
     */
    public abstract void release();

    /**
     * Returns true if resource is already released.
     *
     * @return true if resource is already released, false otherwise
     */
    public abstract boolean isReleased();
}
